<?php

define("LIGHT_BLUE",	"#369");


function _colors($colors){
	$step = round(100/(count($colors)-1));
	for($i=0; $i<count($colors); $i++) {
		$args .= $colors[$i] . " " . ($i*$step) ."%,";
	}
	return preg_replace('/,$/', "", $args);
}
function linear_gradient() {
	$arguments = func_get_args();
	$direction = $arguments[0];
	$colors = array_slice($arguments, 1);
	$agent = get_user_agent();
	
	switch($agent) {
		case "firefox":
			$css = "-moz-linear-gradient(" . implode(",", $colors) . ")";
			break;
		case "chrome":
		case "safari":
			$css = "-webkit-linear-gradient($direction," . _colors($colors) . ")";
			break;
		case "opera":
			$css = "-o-linear-gradient($direction," . _colors($colors) . ")";
			break;
		case "ie10":
		case "ie11":
			$css = "-ms-linear-gradient($direction," . _colors($colors) . ")";
			break;
		default:
			$css = "background:" . $colors[0];
			break;
	}
	
	return "$css /* $agent */";
}

function placeholder($css) {
	switch($agent = get_user_agent()) {
		default:
			$pattern = "::-placeholder{%s}";
			break;
			
		case "firefox":
			$pattern = ":-moz-placeholder{%s}";
			break;
			
		case "chrome":
		case "safari":
			$pattern = "::-webkit-input-placeholder{%s}";
			break;
			
	}
	
	return sprintf($pattern, $css) . "/* $agent */";
}


//=========================================================================================================

function get_user_agent() {
	$a = strtolower($_SERVER['HTTP_USER_AGENT']);
	
	if(strstr($a, "firefox")) {
		return "firefox";
	}elseif(strstr($a, "chrome")) {
		return "chrome";
	}elseif(strstr($a, "msie")) {
		preg_match('/msie (\d+)/', $a, $version);
		return "ie" . $version[1];
	}elseif(strstr($a, "opera")) {
		return "opera";
	}elseif(strstr($a, "safari")) {
		return "safari";
	}else{
		return $a;
	}
}

header("Content-Type:text/css;charset=utf-8");
header("Content-Disposition:inline;filename=style.css");

$filename = @$_GET["file"];
if(!$filename) die("/* not found */");

$data = file_get_contents($filename);

$matches = preg_split('/(\$\{.*?\})/', $data, -1, PREG_SPLIT_DELIM_CAPTURE);
$css = "";
foreach($matches as $item) {
	if(preg_match('/\$\{.*?\}/', $item)) {
		$code = preg_replace('/^\$\{|\}$/', "", $item);
		
		// function
		if(strstr($code,'(')) {
			$function = substr($code, 0, strpos($code,'('));
			if(function_exists($function)) {
				eval('$css .= ' . $code . ';');
			}else {
				$css .= "''; /* $function() NOT FOUND */";
			}
		// variable
		}else if(constant($code)) {
			$css .= constant($code);
		}else{
			$css .= "'' /*$code NOT FOUND*/";
		}
	}else{
		$css .= $item;
	}
}
echo $css;
?>